package org.space.galaxyCabinet.dao.impl;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.space.galaxyCabinet.EmqxClientWrapper;
import org.space.galaxyCabinet.constant.EmqxConstants;
import org.space.galaxyCabinet.domain.Device;
import org.space.galaxyCabinet.dao.DeviceControlDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class DeviceControlDaoImpl implements DeviceControlDao {

    @Autowired
    EmqxClientWrapper emqxClientWrapper;

    @Override
    public boolean updateStatus(Device device) {
        try {
            String topic = EmqxConstants.DEVICE_ALL_CONTROL.replace("all", device.getDeviceNo());
            log.info("topic: {}", topic);
            emqxClientWrapper.publish(topic, JSON.toJSONString(device));
            log.info("mqttData: {}", JSON.toJSONString(device));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean TestFreightChannel(Device device) {
        return false;
    }
}
